package src.queries;

import java.awt.Image;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import src.entities.RadRecord;
import src.entities.User;
import java.io.*;


public class SearchQueries extends UnsecuredQueries {

	public SearchQueries(Connection c, SecurityQueries security) {
		super(c, security);

	}

	/** From date and to date may be null
	 * 
	 * @param keywords
	 * @param fromDate
	 * @param toDate
	 * @return
	 * @throws SQLException
	 */

	public ArrayList<RadRecord> search(String keywords,
			java.util.Date fromDate, java.util.Date toDate, User currentUser) throws SQLException{

		ArrayList<RadRecord> results = new ArrayList<RadRecord>();

		if(fromDate == null)
			fromDate = new java.util.Date(0);
		if(toDate == null)
			toDate = new java.util.Date();

		Date fromDateSQL = parseDate(fromDate);
		Date toDateSQL = parseDate(toDate);

		ResultSet rs = getSearchResults(keywords, fromDateSQL, toDateSQL);

		while(rs.next()){
			RadRecord record = new RadRecord();
			record.setDescription(rs.getString("description"));
			record.setDiagnosis(rs.getString("diagnosis"));
			record.setDoctorName(rs.getString("doctor_name"));
			record.setPatientName(rs.getString("patient_name"));
			record.setPrescribingDate(utilDate(rs.getDate("prescribing_date")));
			record.setTestDate(utilDate(rs.getDate("test_date")));
			record.setRadiologistName(rs.getString("radiologist_name"));
			record.setScore(rs.getInt(1)*6 + rs.getInt(2) + rs.getInt(3)*3);
			record.setTestType(rs.getString("test_type"));
			results.add(record);

		}



		results = security.isViewable(results, currentUser);

		return results;
	}
	private ResultSet getSearchResults(String keywords, Date fromDate, Date toDate) throws SQLException{
		PreparedStatement p = makePreparedStatement("SELECT score(1), score(2), score(3), patient_name," +
				" doctor_name, radiologist_name, test_type, prescribing_date, test_date" +
				", diagnosis, description " +
				"FROM radiology_record " +
				"WHERE test_date <= ?" +
				"AND test_date >= ?" +
				"AND(contains(patient_name, ?, 1) > 0 " +
				"OR contains(description, ?, 2) > 0" +
				"OR contains(diagnosis, ?, 3) > 0)" +
		"order by score(1) desc ");
		p.setDate(1, toDate);
		p.setDate(2, fromDate);
		p.setString(3, keywords);
		p.setString(4, keywords);
		p.setString(5, keywords);


		ResultSet rs = p.executeQuery();

		return rs;
	}
	public InputStream getThumbNail(int picID) throws SQLException{
		PreparedStatement ps = makePreparedStatement(
				"SELECT thumbnail " +
				"FROM pacs_images " +
		"WHERE image_id = ?");
		ps.setInt(1, picID);

		ResultSet rs = ps.executeQuery();
		if(rs.next()){
			InputStream input = rs.getBinaryStream(1);
			return input;
		}

		return null;

	}
	public InputStream getRegularSize(int picID) throws SQLException{
		PreparedStatement ps = makePreparedStatement(
				"SELECT regular_size " +
				"FROM pacs_images " +
		"WHERE image_id = ?");
		ps.setInt(1, picID);

		ResultSet rs = ps.executeQuery();
		if(rs.next()){
			InputStream input = rs.getBinaryStream(1);
			return input;
		}

		return null;
	}
	public InputStream getFullSize(int picID) throws SQLException{
		PreparedStatement ps = makePreparedStatement(
				"SELECT full_size " +
				"FROM pacs_images " +
		"WHERE image_id = ?");
		ps.setInt(1, picID);

		ResultSet rs = ps.executeQuery();
		if(rs.next()){
			InputStream input = rs.getBinaryStream(1);
			return input;
		}

		return null;
	}

}
